package com.newpage.module_gather.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.newpage.module_gather.R;
import com.wetao.lib_common.bean.response.ExplainBean;

import java.util.ArrayList;
import java.util.List;


/**
 * 老师讲解来源适配器
 */
public class CommonExplainAdapter extends RecyclerView.Adapter{


    private List<ExplainBean> mData = new ArrayList<>();
    private int selectIndex;
    private Context mContext;
    public CommonExplainAdapter(Context context) {
        this.mContext = context;
        selectIndex = 0;
    }

    public void setData(List<ExplainBean> data){
        if (data!=null){
            selectIndex = 0;
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.gather_explain_item_pop, parent, false);
        return new ExplainTeacherViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ExplainTeacherViewHolder) {
            ((ExplainTeacherViewHolder)holder).bindData(position);
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }


    public int getSelectIndex() {
        return selectIndex;
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
    }

    class ExplainTeacherViewHolder<T> extends RecyclerView.ViewHolder{
        TextView tv_pop;

        public ExplainTeacherViewHolder( View itemView) {
            super(itemView);
           tv_pop = itemView.findViewById(R.id.tv_teacher_pop);
        }
        public void bindData(final int position) {
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onClick(position);
                    tv_pop.setText(mData.get(position).getExplainName());
                    setSelectIndex(position);
                    notifyDataSetChanged();
                }
            });

            if (getSelectIndex()==position){
                tv_pop.setSelected(true);
            }else {
                tv_pop.setSelected(false);
            }
            tv_pop.setText("" + mData.get(position).getExplainName());

        }
    }



    //第一步 定义接口
    public interface OnItemClickListener {
        void onClick(int position);
    }
    private OnItemClickListener listener;
    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
